package client;

import beans.Group;
import beans.User;
import beans.Whiteboard;
import model.UserLoginInfoModel;

import java.util.ArrayList;
import java.util.List;

/**
 * 用户登录会话信息
 */
public class UserSession {

    public static User user = new User();
    private static String user_login_token = "";

    private static List<Group> groupList;
    private static int selectedGroupIndex = -1;
    private static List<Group> filteredGroupList = new ArrayList<Group>();
    private static boolean isGroupFiltered = false;
    private static String groupFilterWord;


    private static List<Whiteboard> whiteboardList;
    private static int selectedWhiteboardIndex = -1;

    private static List<User> memberList;
    private static int selectedMemberIndex;

    public static void initLoginSession(UserLoginInfoModel userLoginInfoModel) {
        user = userLoginInfoModel.getUser();
        user_login_token = userLoginInfoModel.getToken();
    }

    public static void clearLoginSession() {
        user = new User();
        user_login_token = "";
    }

    public static void setGroupList(List<Group> groupList){
        UserSession.groupList = groupList;
        if (isGroupFiltered){
            filteredGroupList.clear();
            for (int i = 0; i < groupList.size(); i++){
                if (groupList.get(i).getGroup_name().indexOf(groupFilterWord) != -1){
                    filteredGroupList.add(groupList.get(i));
                }
            }
        }
    }

    public static String[] getWhiteboardNameList(){
        String whiteboards[] = new String[whiteboardList.size()];
        for (int i = 0; i < whiteboards.length; i++)
            whiteboards[i] = "  " + whiteboardList.get(i).getBoard_name();
        return whiteboards;
    }

    public static String[] getGroupNameList(){
        List<Group> goups = isGroupFiltered ? filteredGroupList : groupList;
        String group_name_list[] = new String[goups.size()];
        for (int i = 0; i < group_name_list.length; i++)
            group_name_list[i] = goups.get(i).getGroup_name();
        return group_name_list;
    }
    public static void setGroupFilterWord(String groupFilterWord){
        isGroupFiltered = true;
        filteredGroupList.clear();
        selectedGroupIndex = -1;
        UserSession.groupFilterWord = groupFilterWord;
    }
    public static void cancelGroupFilter(){
        isGroupFiltered = false;
        filteredGroupList.clear();
        selectedGroupIndex = -1;
    }

    public static boolean isGroupListEmpty(){
        if (isGroupFiltered)
            return filteredGroupList.isEmpty();
        return groupList.isEmpty();
    }
    public static void setSelectedWhiteboardIndex(int index){
        if (index >= whiteboardList.size())
            selectedWhiteboardIndex = -1;
        else
            selectedWhiteboardIndex = index;
    }
    public static void setSelectedGroupIndex(int index){
        selectedGroupIndex = index;
        if (isGroupFiltered && index >= filteredGroupList.size())
            selectedGroupIndex = -1;
        if (!isGroupFiltered && index >= groupList.size())
            selectedGroupIndex = -1;
    }

    public static void setMemberList(List<User> memberList){
        UserSession.memberList = memberList;
    }

    public static String[] getMemberNameList(){
        String member_name_list[] = new String[memberList.size()];
        for (int i = 0; i < member_name_list.length; i++)
            member_name_list[i] = memberList.get(i).getAccount();
        return member_name_list;
    }

    public static void setSelectedMemberIndex(int index){
        if (index >= memberList.size())
            selectedMemberIndex = -1;
        else
            selectedMemberIndex = index;
    }
    public static void clearGroupSelection(){
        selectedGroupIndex = -1;
    }
    public static void clearWhiteboardSelection(){
        selectedWhiteboardIndex = -1;
    }
    public static void clearMemberSelection(){
        selectedMemberIndex = -1;
    }

    public static boolean isGroupSelectionValid(){
        return selectedGroupIndex != -1;
    }
    public static boolean isWhiteboardSelectionValid(){
        return selectedWhiteboardIndex != -1;
    }
    public static boolean isMemberSelectionValid(){
        return selectedMemberIndex != -1;
    }

    public static User getSelectedMember(){
        return memberList.get(selectedMemberIndex);
    }

    public static Group getSelectedGroup(){
        if (isGroupFiltered)
            return filteredGroupList.get(selectedGroupIndex);
        return groupList.get(selectedGroupIndex);
    }

    public static void setWhiteboardList(List<Whiteboard> whiteboardList){
        UserSession.whiteboardList = whiteboardList;
    }

    public static Whiteboard getSelectedWhiteboard(){
        return whiteboardList.get(selectedWhiteboardIndex);
    }

    public static int getGroupMemberAmount(){
        if (memberList == null)
            return 0;
        return memberList.size();
    }

    public static long LoginUser() {
        return user.getId();
    }

    public static String UserAccount() {
        return user.getAccount();
    }

    public static String Token() {
        return user_login_token;
    }


}
